/*
 * @Description  : 生成路由表
 * @Author       : Keyman
 * @Date         : 2025-11-17 16:25:28
 * @LastEditors  : Keyman
 * @LastEditTime : 2025-11-17 19:25:52
 */

import { type RouteObject } from "react-router-dom";
import { compomentMap } from "../router/routerMap";

interface MenuType {
  icon: string;
  label: string;
  key: string;
  children?: MenuType[];
}

export const generatesRoutes = (menu: MenuType[]): RouteObject[] => {
  return menu.map((item: MenuType) => {
    const hasChildren = item.children && item.children.length > 0;
    const routeObj: RouteObject = {
      path: item.key,
      element: hasChildren ? null : compomentMap[item.key], // 如果有子菜单，则将子菜单的组件作为元素
    };

    if (item.children) {
      routeObj.children = generatesRoutes(item.children);
    }

    return routeObj;
  });
};
